/**
 * 游戏功能模块
 */
(function(cjs,an){
    //const cjs=createjs;
    /**
     * 游戏资源预加载模块
     */
    const PreloadModule={
        init:function(){
            console.log('PreloadModule启动')
            //导入
            const {LayerControl,MessageControl,ModuleEvent,PreloadView}=an;
            const layer=LayerControl.main_layer;
            const view=new PreloadView();
            view.parent=layer;
            layer.addChild(view);
            //
            const loader=new cjs.LoadQueue(false);
            loader.installPlugin(cjs.Sound);
            loader.addEventListener('progress',function(evt){
                //刷新加载进度
                view.setProgress(evt.progress);
            });
            loader.addEventListener('fileload',function(evt){
                //console.log(evt);
                var images=an.comp.getImages();	
	            if (evt && (evt.item.type == "image")) { images[evt.item.id] = evt.result; }
            });
            loader.addEventListener('complete',function(evt){
                const comp=an.comp;
                const lib=comp.getLibrary();
                let ss=comp.getSpriteSheet();
                const queue = evt.target;
                var ssMetadata = lib.ssMetadata;
                for(i=0; i<ssMetadata.length; i++) {
                    ss[ssMetadata[i].name] = new cjs.SpriteSheet( {"images": [queue.getResult(ssMetadata[i].name)], "frames": ssMetadata[i].frames} )
                }
                //资源加载完成
                layer.removeChild(view);
                view.parent=null;
                //启动其他模块
                StartModule.init();
                GameingModule.init();
            });
            an.lib.properties.manifest.push(
                {src:"sounds/BG.mp3", id:"BG"},
                {src:"sounds/startBG.mp3",id:"startBG"}
            );
            //console.log(an.lib.properties.manifest);
            an.lib.properties.loader=loader;
            loader.loadManifest(an.lib.properties.manifest);
        }
    }
    /**
     * 开始游戏模块
     */
    const StartModule={
        init:function(){
            console.log('StartModule启动')
            //导入
            const {LayerControl,MessageControl,ModuleEvent,ViewEvent,StartView}=an;
            const layer=LayerControl.main_layer;
            const view=new StartView();
            view.parent=layer;
            layer.addChild(view);
            view.on(ViewEvent.REQUEST_FULL_SCREEN,()=>{
                fullScreen(document.getElementById('canvas'));
            },null);
            view.on(ViewEvent.REQUEST_OUT_FULL_SCREEN,()=>{
                exitFullScreen();
            },null);
            view.on(ViewEvent.START_GAME,()=>{
                layer.removeChild(view);
                view.parent=null;
                MessageControl.send(ModuleEvent.SHOW_GAMEING);
            },null);

            MessageControl.on(ModuleEvent.FULL_SCREEN,(evt)=>{
                view.setSize(evt.data.width,evt.data.height);
                view.fullScreen=true;
                an.lib.properties.fullScreen=true;
            });

            MessageControl.on(ModuleEvent.OUT_FULL_SCREEN,(evt)=>{
                view.setSize(evt.data.width,evt.data.height);
                view.fullScreen=false;
                an.lib.properties.fullScreen=false;
            });
        }
    }
    /**
     * 游戏模块
     */
    const GameingModule={
        init:function(){
            console.log('GameingModule启动');
            //导入
            const {LayerControl,MessageControl,ModuleEvent,ViewEvent,GameingView}=an;
            const layer=LayerControl.main_layer;
            const view=new GameingView();

            MessageControl.on(ModuleEvent.SHOW_GAMEING,()=>{
                view.parent=layer;
                layer.addChild(view);
                cjs.Sound.play("BG");
                view.run();
            });
        }
    }

    an.PreloadModule=PreloadModule;
    //an.StartModule=StartModule;
})(createjs = createjs||{}, AdobeAn = AdobeAn||{});
var createjs, AdobeAn;